package com.bilgeadam.swing.view;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import com.bilgeadam.swing.view.widget.Button;

/**
 *
 * @author Bora SAYINER
 * @since Dec 20, 2014 11:48:34 AM
 * @version 1.0
 */
public class LoginFrame implements GenericView {

	private JFrame frmMain;
	private JTextField txtUserName;
	private JPasswordField txtPassword;
	private JButton btnLogin;
	private JButton btnExit;
	private JPanel pnlCenter;

	public LoginFrame() {
		initalizeComponents();
	}

	private void initalizeComponents() {
		this.frmMain = new JFrame();
		frmMain.setTitle("Login Sample");
		frmMain.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frmMain.setResizable(false);
		{
			{
				pnlCenter = new JPanel();
				GridBagLayout gridBagLayout = new GridBagLayout();
				GridBagConstraints constraints = new GridBagConstraints();
				constraints.fill = GridBagConstraints.HORIZONTAL;
				constraints.insets = new Insets(5, 5, 5, 5);
				pnlCenter.setLayout(gridBagLayout);

				pnlCenter.setBorder(BorderFactory.createTitledBorder("Login"));
				{
					{
						JLabel lblUserName = new JLabel();
						lblUserName.setText("User Name");
						constraints.gridx = 0;
						constraints.gridy = 0;
						pnlCenter.add(lblUserName, constraints);
					}
					{
						txtUserName = new JTextField(20);
						constraints.gridx = 1;
						constraints.gridy = 0;
						constraints.weightx = 1;
						pnlCenter.add(txtUserName, constraints);
					}
					{
						JLabel lblPassword = new JLabel();
						lblPassword.setText("Password");
						constraints.gridx = 0;
						constraints.gridy = 1;
						pnlCenter.add(lblPassword, constraints);
					}
					{
						txtPassword = new JPasswordField();
						constraints.gridx = 1;
						constraints.gridy = 1;
						constraints.weightx = 1;
						pnlCenter.add(txtPassword, constraints);

					}
				}
				frmMain.add(pnlCenter, BorderLayout.CENTER);
			}
			{
				JPanel pnlSouth = new JPanel();
				{
					{
						btnLogin = new Button();
						btnLogin.setText("Login");
						pnlSouth.add(btnLogin);
					}
					{
						btnExit = new Button();
						btnExit.setText("Exit");
						;
						pnlSouth.add(btnExit);
					}
				}
				frmMain.add(pnlSouth, BorderLayout.SOUTH);
			}
		}
		frmMain.pack();
		frmMain.setLocationRelativeTo(null);
	}

	@Override
	public String getUserName() {
		return txtUserName.getText();
	}

	@Override
	public String getPassword() {
		return new String(txtPassword.getPassword());
	}

	@Override
	public void registerLoginButton(ActionListener listener) {
		btnLogin.addActionListener(listener);
	}

	@Override
	public void setVisible(boolean visible) {
		frmMain.setVisible(visible);
	}

	@Override
	public void registerExitButton(ActionListener listener) {
		btnExit.addActionListener(listener);
	}

	@Override
	public void clearAllTextFields() {
		Component[] components = pnlCenter.getComponents();
		for (int i = 0; i < components.length; i++) {
			Component currentComponent = components[i];
			if (currentComponent instanceof JTextField) {
				JTextField textField = (JTextField) currentComponent;
				textField.setText("");
			}
		}
		
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	

}
